package com.example.administrator.idocment.docment;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.administrator.idocment.BaseTypeFind;
import com.example.administrator.idocment.R;
import com.example.administrator.idocment.docment.tool.CheckEnum;
import com.example.administrator.idocment.msgpack.modelpack.DocModel;
import com.example.administrator.idocment.tooltime.TimerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/15 0015.
 */
public class DocFind extends BaseTypeFind<DocModel> {


    // public static final String[] docType=new String[]{"application/msword","application/pdf","application/mspowerpoint","text/plain","application/vnd.ms-excel"};
    public static final String[] docType=new String[]{".xls",".xlsx"};
    public DocFind(Context context) {
        super(context);
    }

    final String tag=getClass().getName();
    List<DocModel> docModelList=new ArrayList<>();
    @Override
    public void select() {
        Uri uri= MediaStore.Files.getContentUri("external");
        String[] colums=new String[]{MediaStore.Files.FileColumns.DATA,MediaStore.Files.FileColumns.SIZE,MediaStore.Files.FileColumns.MIME_TYPE};
        String selection="";
        for(int i=0;i<docType.length;i++)
        {
            if(i!=0)
            {
                selection+=" OR ";
            }
            selection = selection + MediaStore.Files.FileColumns.DATA+ " LIKE '%" + docType[i] + "'";
        }
        String sort=MediaStore.Files.FileColumns.SIZE+" DESC";

       Cursor cursor= contentResolver.query(uri, colums, selection, null, sort);
        try {
            cursor.moveToFirst();
            do {
                String path=cursor.getString(0);
                android.util.Log.e(tag,path);
                File file=new File(path);
                String title=file.getName();
                String date= TimerFactory.create(TimerFactory.YearFormat).paseTimer(file.lastModified());
                String size=getFileSzie(cursor.getLong(1));
                String mimeType=cursor.getString(2);

                int imgResouce = 0;
                switch (CheckEnum.getType(mimeType, file))
                {
                    case doc://word
                        imgResouce= R.mipmap.ic_doc;
                        break;
                    case pdf://pdf
                        imgResouce=R.mipmap.ic_pdf;
                        break;
                    case ppt://ppt
                        imgResouce=R.mipmap.ic_ppt;
                        break;
                    case txt://text
                        imgResouce=R.mipmap.ic_txt;
                        break;
                    case excel://excel
                        imgResouce=R.mipmap.ic_excel;
                        break;
                }



                DocModel audioModel=new DocModel();
                audioModel.setPath(path);
                audioModel.setDate(date);
                audioModel.setSize(size);
                audioModel.setTitle(title);
                audioModel.setImgResource(imgResouce);
                docModelList.add(audioModel);


            }while (cursor.moveToNext());
        }
        catch (Exception e)
        {

        }





    }

    @Override
    public List<DocModel> getListInfo() {
        return docModelList;
    }



}
